package com.software.adapterview.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.software.adapterview.R;
import com.software.adapterview.entity.Wechat;

import java.util.List;

/**
 * 获取数据
 * 给视图绑定数据
 */
public class WechatAdapter extends BaseAdapter {
    private Context context;
    private Integer layoutId;// item_wechat布局
    private List<Wechat> wechatList;
    public WechatAdapter(Context context, Integer layoutId, List<Wechat> wechatList) {
        this.context = context;
        this.layoutId = layoutId;
        this.wechatList = wechatList;
    }

    //获取数据源中数据的个数

    @Override
    public int getCount() {
        return wechatList.size();
    }

    //获取数据，获取position下标位置的数据
    @Override
    public Object getItem(int position) {
        return wechatList.get(position);
    }

    //返回数据下标的位置
    @Override
    public long getItemId(int position) {
        return position;
    }


    //拿到一个未绑定数据的视图
    //绑定数据
    //返回绑定好数据的视图
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            //LayoutInflater视图加载器
            convertView = LayoutInflater.from(context).inflate(layoutId, null);
        }
        //获取控件
        ImageView iv_avatar = convertView.findViewById(R.id.iv_avatar);
        TextView  tv_nickname = convertView.findViewById(R.id.tv_nickname);
        TextView  tv_endMsg = convertView.findViewById(R.id.tv_endMsg);
        Wechat wechat = wechatList.get(position);
        iv_avatar.setImageResource(wechat.getAvatar());
        tv_nickname.setText(wechat.getNickname());
        tv_endMsg.setText(wechat.getEndMessage());
        return convertView;
    }
}
